#include "CTableSession.h"

CTableSession::CTableSession(QObject *parent)
    : AbsTable("session",parent)
{
    if(!this->exisit())
        createTable();

    columnNames();
}

CTableSession *CTableSession::instance()
{
    static CTableSession *pInstance = nullptr;
    if(nullptr == pInstance){
        pInstance = new CTableSession();
    }

    return pInstance;
}

bool CTableSession::createTable()
{
    QString strSql =  "CREATE TABLE session (                    "
                      "    id               CHAR (50) PRIMARY KEY"
                      "                               UNIQUE     "
                      "                               NOT NULL,  "
                      "    name             TEXT,                "
                      "    active           BLOB,                "
                      "    create_time      TEXT,                "
                      "    close_time       TEXT,                "
                      "    using_time       INT,                 "
                      "    text_code        TEXT DEFAULT [UTF-8],"
                      "    session_type     TEXT,                "
                      "    ip               TEXT,                "
                      "    port             INT,                 "
                      "    seial_name       TEXT,                "
                      "    baude            INT,                 "
                      "    data_bit         INT,                 "
                      "    parity           TEXT,                "
                      "    stop_bit         TEXT,                "
                      "    flowctrl         TEXT,                "
                      "    r_text_url       TEXT,                "
                      "    r_is_hex         BLOB,                "
                      "    r_show_send      BLOB,                "
                      "    r_show_timestamp BLOB,                "
                      "    r_start          BLOB,                "
                      "    s_text_url       TEXT,                "
                      "    s_is_hex         BLOB,                "
                      "    s_auto_enable    BLOB,                "
                      "    s_auto_span      INT                  "
                      ");                                        ";

    return execSql(strSql);
}

bool CTableSession::addRow(StSession &st)
{
    QString strSql = "insert into " + m_strTableName + " values(";
    for(int i = 0; i < m_strListColumnNames.count(); i++){
        strSql += "?,";
    }
    strSql.chop(1); //删除最后一个字符
    strSql += ")";

    QSqlQuery query(*dbConnection());
    query.prepare(strSql);

    query.addBindValue(st.id);
    query.addBindValue(st.name);
    query.addBindValue(st.active);
    query.addBindValue(st.create_time);
    query.addBindValue(st.close_time);
    query.addBindValue(st.using_time);
    query.addBindValue(st.text_code);
    query.addBindValue(st.session_type);
    query.addBindValue(st.ip);
    query.addBindValue(st.port);
    query.addBindValue(st.seial_name);
    query.addBindValue(st.baude);
    query.addBindValue(st.data_bit);
    query.addBindValue(st.parity);
    query.addBindValue(st.stop_bit);
    query.addBindValue(st.flowctrl);
    query.addBindValue(st.r_text_url);
    query.addBindValue(st.r_is_hex);
    query.addBindValue(st.r_show_send);
    query.addBindValue(st.r_show_timestamp);
    query.addBindValue(st.r_start);
    query.addBindValue(st.s_text_url);
    query.addBindValue(st.s_is_hex);
    query.addBindValue(st.s_auto_enable);
    query.addBindValue(st.s_auto_span);

    return exec(query);
}

bool CTableSession::updateRow(StSession &st)
{
    QString strSql = "update " + m_strTableName + " set ";
    for(int i = 1; i < m_strListColumnNames.count(); i++){
        strSql += QString("%1 = ?,").arg(m_strListColumnNames[i]);
    }
    strSql.chop(1); //删除最后一个字符
    strSql += " where id = ?";

    QSqlQuery query(*dbConnection());
    query.prepare(strSql);

//    query.addBindValue(st.id);
    query.addBindValue(st.name);
    query.addBindValue(st.active);
    query.addBindValue(st.create_time);
    query.addBindValue(st.close_time);
    query.addBindValue(st.using_time);
    query.addBindValue(st.text_code);
    query.addBindValue(st.session_type);
    query.addBindValue(st.ip);
    query.addBindValue(st.port);
    query.addBindValue(st.seial_name);
    query.addBindValue(st.baude);
    query.addBindValue(st.data_bit);
    query.addBindValue(st.parity);
    query.addBindValue(st.stop_bit);
    query.addBindValue(st.flowctrl);
    query.addBindValue(st.r_text_url);
    query.addBindValue(st.r_is_hex);
    query.addBindValue(st.r_show_send);
    query.addBindValue(st.r_show_timestamp);
    query.addBindValue(st.r_start);
    query.addBindValue(st.s_text_url);
    query.addBindValue(st.s_is_hex);
    query.addBindValue(st.s_auto_enable);
    query.addBindValue(st.s_auto_span);

    query.addBindValue(st.id);

    return exec(query);
}

bool CTableSession::updateColumn(QString strId, Column nCol, QVariant var)
{
//    if(nCol >= m_strListColumnNames.count()){
//        qWarning()<<"列索引越界:"<<nCol;
//        return false;
//    }
     QString strSql = QString("update %1 set %2=? where id=?")
             .arg(m_strTableName,m_strListColumnNames[nCol]);

     QSqlQuery query(*dbConnection());
     query.prepare(strSql);
     query.addBindValue(var);
     query.addBindValue(strId);

     return exec(query);
}

bool CTableSession::deleteRow(QString strId)
{
    QString strSql = QString("delete from %1 where id='%2'").arg(m_strTableName,strId);

    QSqlQuery query(*dbConnection());
    query.prepare(strSql);

    return exec(query);
}

bool CTableSession::deleteHistory()
{
    QString strSql = QString("delete from %1 where active=0").arg(m_strTableName);

    QSqlQuery query(*dbConnection());
    query.prepare(strSql);

    return exec(query);
}

bool CTableSession::deleteAll()
{
    QString strSql = QString("delete from %1").arg(m_strTableName);

    QSqlQuery query(*dbConnection());
    query.prepare(strSql);

    return exec(query);
}

StSession CTableSession::queryRow(QString strId)
{
    QString strSql = QString("select * from %1 where id='%2'").arg(m_strTableName,strId);

    QSqlQuery query(*dbConnection());
    query.prepare(strSql);

    exec(query);


    QVariantList varList;
    while(query.next()){
        for(int i = 0;i<query.record().count();i++){
            varList.append(query.value(i));
        }
    }

    return StSession().loadData(varList);
}

QVector<StSession> CTableSession::queryAll()
{
    QString strSql = QString("select * from %1").arg(m_strTableName);

    QSqlQuery query(*dbConnection());
    query.prepare(strSql);

    QVector<StSession> vecData;
    if(exec(query)){
        while(query.next()){
            QVariantList varList;
            for(int i = 0;i<query.record().count();i++){
                varList.append(query.value(i));
            }

            vecData.append(StSession().loadData(varList));
        }
    }

    return vecData;
}

QVector<StSession> CTableSession::queryHistory()
{
    QString strSql = QString("select * from %1 where active=0").arg(m_strTableName);

    QSqlQuery query(*dbConnection());
    query.prepare(strSql);

    QVector<StSession> vecData;
    if(exec(query)){
        while(query.next()){
            QVariantList varList;
            for(int i = 0;i<query.record().count();i++){
                varList.append(query.value(i));
            }

            vecData.append(StSession().loadData(varList));
        }
    }

    return vecData;
}
